package com.ktwlsoft.archivemanage.controller.businessInspect;

import com.ktwlsoft.archivemanage.domain.business.businessService.businessInspect.BusinessInspectRandomizingService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.OperationRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.OperationLog;
import com.ktwlsoft.archivemanage.domain.dao.entity.businessInspect.BusinessInspectRandomizing;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.businessInspect.BusinessInspectRandomizingSaveRequest;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveLogRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;

import javax.validation.Valid;
import java.util.List;


/**
 * business_inspect_randomizing--业务检查随机抽取控制层
 *
 * @author zhuhe
 * @date 2023/06/25
 */
@Api(value = "业务检查-随机抽取", description = "业务检查-随机抽取",tags = "1.2")
@RestController
@RequestMapping(value = "businessInspectRandomizing")
@RequiredArgsConstructor
public class BusinessInspectRandomizingController {

    private final BusinessInspectRandomizingService service;

    /**
     * 保存或更新 on 2019/10/28 9:32
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "保存或更新")
    @ResponseBody
    @PostMapping("saveOrUpdate")
    public ResponseEntity<?> saveOrUpdate(@Valid @RequestBody List<BusinessInspectRandomizingSaveRequest> request) {
        Result<?> result = service.saveOrUpdate(request);
        if (result.getResultType().equals(ResultType.BUSINESS_SUCCESS)) {
            saveLog(OperationLog.OperationType.ADD, "设置随机抽查");
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 列表 on 2019/10/28 9:32
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "列表")
    @GetMapping("findList")
    public ResponseEntity<?> findList(@ApiParam(name = "businessId",value = "业务id") @RequestParam(defaultValue = "") String businessId,
                                      @ApiParam(name = "businessType",value = "业务类型:PERSONNEL(人员),UNIT(单位)") @RequestParam(defaultValue = "") BusinessInspectRandomizing.businessType businessType) {
        Result<?> result = service.findList(businessId,businessType);
        return ResponseEntity.ok(result);
    }

    /**
     * 随机抽取单位 on 2019/10/28 9:32
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "随机抽取单位")
    @GetMapping("randomizingUnit")
    public ResponseEntity<?> randomizingUnit(@ApiParam(name = "num",value = "数量") @RequestParam Integer num) {
        return ResponseEntity.ok(service.randomizingUnit(num));
    }

    /**
     * 随机抽取人员 on 2019/10/28 9:32
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "随机抽取人员")
    @GetMapping("randomizingPersonnel")
    public ResponseEntity<?> randomizingPersonnel(@ApiParam(name = "num",value = "数量") @RequestParam Integer num) {
        return ResponseEntity.ok(service.randomizingPersonnel(num));
    }

    /**
     * 新增日志 on 2019/11/14 10:37
     *
     * @param operationType 操作类型
     * @param description   描述
     */
    private void saveLog(OperationLog.OperationType operationType, String description) {
        OperationRepositoryService.saveLog(new SaveLogRequest(
                operationType,
                description,
                "业务检查",
                OperationLog.BusinessType.BUSINESS
        ));
    }
}
